<template>
  <div>
    <!-- 上半部分 -->
    <el-card style="margin: 20px 0">
      <CategorySelect @getCategoryId="getCategoryId" :show="scene != 0"></CategorySelect>
    </el-card>

    <!-- 下半部分 -->
    <el-card>
      <!-- 第一块，展示Spu列表 -->
      <div v-show="scene === 0">
        <el-button type="primary" icon="rl-icon-plus" style="margin-bottom: 20px" :disabled="!c3Id" @click="addSpu">添加Spu</el-button>
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width"></el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width"></el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-button type="success" icon="el-icon-plus" size="mini" title="添加sku" @click="addSku(row)"></el-button>
              <el-button type="warning" icon="el-icon-edit" size="mini" title="编辑spu" @click="updataSpu(row)"></el-button>
              <el-button type="info" icon="el-icon-info" size="mini" title="查看当前spu的所有sku列表" @click="showSkuList(row)"></el-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="deleteSpu(row)" style="margin: 0 10px">
                <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini" title="删除spu"></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[1, 5, 10]"
          :page-size="limit"
          layout=" prev, pager, next, jumper,->,total, sizes"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        ></el-pagination>
      </div>

      <!-- 第二块，添加/修改Spu -->
      <SpuForm v-show="scene === 1" @changeScene="changeScene" ref="spuForm"></SpuForm>

      <!-- 第三块，添加sku -->
      <SkuForm v-show="scene === 2" ref="skuForm" @changeScene1="changeScene1"></SkuForm>
    </el-card>
    <!-- sku列表的dialog -->
    <el-dialog :title="`${spu.spuName}的Sku列表`" :visible.sync="dialogTableVisible" :before-close="beforeClose">
      <el-table :data="skuList" style="width: 100%" border v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="width"></el-table-column>
        <el-table-column prop="price" label="价格" width="width"></el-table-column>
        <el-table-column prop="weight" label="重量" width="width"></el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{row,$index}">
            <img :src="row.skuDefaultImg" alt="暂无" style="width:100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
// 引入子组件
import SpuForm from './SpuForm'
import SkuForm from './SkuForm'

export default {
  name: 'Spu',
  components: { SpuForm, SkuForm },
  data() {
    return {
      c1Id: '',
      c2Id: '',
      c3Id: '',
      page: 1,
      limit: 1,
      total: 0,
      // 用于存放获取数据的结构
      records: [],
      // 用于控制显示哪个页面
      scene: 0,
      // 用于控制sku列表的显示
      dialogTableVisible: false,
      // 获取当前的row
      spu: {},
      // 获取服务器传来的skuList
      skuList: [],
      // 控制loading动画
      loading: true
    }
  },
  methods: {
    // 分页器相关
    handleCurrentChange(page) {
      this.page = page
      this.getSpuList()
    },
    handleSizeChange(limit) {
      this.limit = limit
      this.getSpuList()
    },
    // 获取子组件传送来的各级分类 id
    getCategoryId({ cId, level }) {
      if (level === 1) {
        this.c1Id = cId
        this.c2Id = ''
        this.c3Id = ''
      } else if (level === 2) {
        this.c2Id = cId
        this.c3Id = ''
      } else if (level === 3) {
        this.c3Id = cId
        // 当获取到3级id后，生成下方表单数据
        this.getSpuList()
      }
    },
    // 获取Spu列表数据
    async getSpuList() {
      const { page, limit, c3Id } = this
      const result = await this.$API.spu.reqSpuList(page, limit, c3Id)
      if (result.code === 200) {
        // console.log(result)
        this.records = result.data.records
        this.total = result.data.total
      }
    },
    // 添加Spu 按钮的回调
    addSpu() {
      this.scene = 1
      // 通知子组件发送请求
      this.$refs.spuForm.addSpuData(this.c3Id)
    },
    // 修改Spu 按钮的回调
    updataSpu(row) {
      this.scene = 1
      // 调用子组件中的方法
      this.$refs.spuForm.initSpuFormData(row)
    },
    // 删除Spu 按钮的回调
    async deleteSpu(row) {
      const result = await this.$API.spu.reqDeleteSpu(row.id)
      if (result.code === 200) {
        this.$message('删除成功')
        this.getSpuList()
      }
    },
    // 由spuform返回主页面额回调
    changeScene({ scene, flag }) {
      this.scene = scene
      // 点击保存返回后，重新渲染列表
      if (flag == '添加') this.page = 1
      this.getSpuList()
    },
    // 添加Sku 按钮的回调
    addSku(row) {
      this.scene = 2
      this.$refs.skuForm.getSkuData(this.c1Id, this.c2Id, row)
    },
    // 由skuform返回主页面的回调
    changeScene1(scene) {
      this.scene = scene
    },
    // 点击查看所有Sku按钮的回调函数
    async showSkuList(row) {
      this.dialogTableVisible = true
      this.spu = row
      // 发送请求，获取sku列表的数据
      const result = await this.$API.spu.reqSkuList(row.id)
      if (result.code === 200) {
        this.skuList = result.data
      }
      // 加载完毕数据后，关闭loading动画
      this.loading = false
    },
    // dialog关闭前的回调函数
    beforeClose(done) {
      // 开启loading属性
      this.loading = true
      // 清空当前sku列表的数据
      this.skuList = []
      // 最后关闭dialog
      done()
    }
  }
}
</script>

<style></style>
